package com.boy.rpc.framework.console;


import com.boy.rpc.framework.console.exception.RedisInitException;
import com.boy.rpc.framework.console.utils.InitRedisConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.apache.commons.lang3.StringUtils;
import java.util.*;



/**
 * Created by wesley on 2017-03-13.
 */
public class RedisApplication implements ApplicationContextAware{

    private static Logger logger = LoggerFactory.getLogger(RedisApplication.class);

    private Properties properties;

    public RedisApplication(){

    }

    public RedisApplication(Properties properties){
        this.properties = properties;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.debug("Init Redis Servers start");
        logger.debug("properties->>"+properties);
        String currentServerName = "";
        try {
            int serverNum = Integer.parseInt(properties.getProperty(Constant.REDIS_PROPERTIES_SERVER_NUM_KEY));
            String keys = properties.getProperty(Constant.REDIS_PROPERTIES_KEYS_PRXFIX_KEY);
            for(int i=1;i<=serverNum;i++) {
                String name = properties.getProperty(Constant.REDIS_PROPERTIES_NAME_PREFIX_KEY + i);
                String host = properties.getProperty(Constant.REDIS_PROPERTIES_HOST_PREFIX_KEY + i);
                int port = Integer.parseInt(properties.getProperty(Constant.REDIS_PROPERTIES_PORT_PREFIX_KEY + i));
                String password = properties.getProperty(Constant.REDIS_PROPERTIES_PASSWORD_PREFIX_KEY + i);
                currentServerName = host;
                InitRedisConnection.createRedisConnection(name, host, port, password, keys);
            }
            logger.debug("currentServerName---->>"+currentServerName);
        } catch (NumberFormatException e) {
            logger.error("initRedisServers: " + currentServerName+" occur NumberFormatException :" + e.getMessage());
            throw new RedisInitException(e);
        } catch (Throwable e1) {
            logger.error("initRedisServers: " + currentServerName+" occur Throwable :" + e1.getMessage());
            throw new RedisInitException(currentServerName + " init failed", e1);
        }
    }


    /**
     * 创建redis连接
     * @param name
     * @param host
     * @param port
     * @param password
     */
    /*public void createRedisConnection(String name, String host, int port, String password, String keys) {
        logger.debug("createRedisConnection start...");
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setHostName(host);
        connectionFactory.setPort(port);
        if(!StringUtils.isEmpty(password))
            connectionFactory.setPassword(password);
        connectionFactory.afterPropertiesSet();

        //保存RedisTemplate
        RedisTemplate redisTemplate = new MyStringRedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.afterPropertiesSet();
        Constant.redisTemplatesMap.put(name, redisTemplate);

        //把服务写入内存缓存中
        Map<String, Object> redisServerMap = new HashMap<String, Object>();
        redisServerMap.put("name", name);
        redisServerMap.put("host", host);
        redisServerMap.put("port", port);
        redisServerMap.put("password", password);
        Constant.redisServerCache.add(redisServerMap);

        //初始化redis-keys缓存
        InitKeysCacheUtil.initRedisKeysCache(redisTemplate, name, keys);

        RedisZtreeUtil.initRedisNavigateZtree(name);

        logger.debug("redisTemplatesMap--->>"+Constant.redisTemplatesMap+"redisServerCache--->"+Constant.redisServerCache);

    }*/



}
